#include "client_test.h"

#include <string>

namespace Ballade
{
	namespace SmartClient
	{
		using std::string;

		CPPUNIT_TEST_SUITE_REGISTRATION(ClientTest);

		void ClientTest::setUp()
		{
			_client = new Client("127.0.0.1", 1316);
		}

		void ClientTest::tearDown()
		{
			_client->close();
			delete _client;
		}

		void ClientTest::testGetIPAddress()
		{
			string ipaddress("127.0.0.1");
			CPPUNIT_ASSERT(strncmp(ipaddress.c_str(), _client->getIPAddress(), ipaddress.length()) == 0);
		}

		void ClientTest::testGetPort()
		{
			CPPUNIT_ASSERT(1316 == _client->getPort());
		}

		void ClientTest::testSetIPAddress()
		{
			string ipaddress("127.0.0.2");
			_client->setIPAddress(ipaddress.c_str());
			CPPUNIT_ASSERT(strncmp(ipaddress.c_str(), _client->getIPAddress(), ipaddress.length()) == 0);
		}

		void ClientTest::testSetPort()
		{
			unsigned short port = 1317;
			_client->setPort(port);
			CPPUNIT_ASSERT(port == _client->getPort());
		}

		void ClientTest::testIsConnected()
		{
			CPPUNIT_ASSERT(!_client->isConnected());
		}

		void ClientTest::testConnect()
		{
			try
			{
				_client->connect();	
				_client->close();
				CPPUNIT_ASSERT(false);
			}
			catch(Network::SocketError &e)
			{
				CPPUNIT_ASSERT(true);
			}

			_client->setPort(135);
			try
			{
				_client->connect();
				CPPUNIT_ASSERT(_client->isConnected());
				_client->close();
			}
			catch(Network::SocketError &e)
			{
				CPPUNIT_ASSERT(false);
			}
		}
	}
}